package com.wendy.common.core.json.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonStreamContext;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import com.wendy.common.core.constant.BaseEnum;

import java.io.IOException;

/**
 * Standard serializer used for {@link MyAnnotationIntrospector} types.
 *<p>
 * Based on {@link StdScalarSerializer} since the JSON value is
 * scalar (String).
 */
@JacksonStdImpl
public class MyDicSerializer extends JsonSerializer<BaseEnum>
{

    @Override
    public void serialize(BaseEnum value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeString(String.valueOf(value.getCode()));
        JsonStreamContext code = gen.getOutputContext();
        String currentName=code.getCurrentName();
        gen.writeFieldName(currentName+"Name");
        gen.writeString(value.getName());
    }
}
